﻿#region 文件信息

/*----------------------------------------------------------------
//
// 文件名称：
// 文件功能描述：
// 设计要求：
//
// 文 件 名：    VampirewalServiceProviderFactory
// 创建者：      杨程
// 创建日期：	    2022/12/29 12:49:43

//----------------------------------------------------------------*/

#endregion

namespace Vampirewal.Core.Components;

/// <summary>
/// IoC工厂类
/// </summary>
[Obsolete("未启用", true)]
internal partial class VampirewalServiceProviderFactory : IServiceProviderFactory<IocContainer>
{
    /// <summary>
    ///
    /// </summary>
    public VampirewalServiceProviderFactory()
    {
        //构造函数
    }

    public IocContainer CreateBuilder(IServiceCollection services)
    {
        return new IocContainer(services);
    }

    public IServiceProvider CreateServiceProvider(IocContainer containerBuilder)
    {
        return new Provider(containerBuilder);
    }
}

#region MyRegion

/// <summary>
/// 工厂容器
/// </summary>
[Obsolete("未启用", true)]
internal class IocContainer
{
    public IocContainer(IServiceCollection serviceDescriptors)
    {
        ServiceDescriptors = serviceDescriptors;
    }

    public IServiceCollection ServiceDescriptors { get; }
}

[Obsolete("未启用", true)]
internal class Provider : IServiceProvider
{
    private ServiceProvider? ServiceProvider { get; set; }

    public IocContainer ContainerBuilder { get; }

    public Provider(IocContainer continerBuilder)
    {
        InitProvider(continerBuilder?.ServiceDescriptors);
        ContainerBuilder = continerBuilder;
    }

    public object GetService(Type serviceType)
    {
        var result = ServiceProvider?.GetService(serviceType);

        if (result == null)
        {
            //result = ContainerBuilder.ServiceDescriptors.
        }

        return result;
    }

    private void InitProvider(IServiceCollection? serviceDescriptors)
    {
        var constructorsMethod = typeof(ServiceProvider).GetConstructors(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).FirstOrDefault();
        if (constructorsMethod != null && serviceDescriptors != null)
        {
            ServiceProvider = constructorsMethod.Invoke(new object[] { serviceDescriptors, new ServiceProviderOptions() }) as ServiceProvider;
        }
    }
}

#endregion

[Obsolete("未启用", true)]
internal class IocBuilder
{
    private class ServiceScope : IServiceScope
    {
        public ServiceScope(IServiceProvider serviceProvider) => ServiceProvider = serviceProvider;

        public IServiceProvider ServiceProvider { get; }

        public void Dispose() => (ServiceProvider as IDisposable)?.Dispose();
    }

    private class ServiceScopeFactory : IServiceScopeFactory
    {
        private readonly Provider _cat;

        public ServiceScopeFactory(Provider cat) => _cat = cat;

        public IServiceScope CreateScope() => new ServiceScope(_cat);
    }
}